package com.tebiecloud.goods.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.tebiecloud.common.utils.BeanConvertUtils;
import com.tebiecloud.goods.client.model.entity.GoodsCategory;
import com.tebiecloud.goods.client.model.vo.GoodsCategoryVO;
import com.tebiecloud.goods.server.service.GoodsCategoryService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 商品品类：分二级 前端控制器
 * @author tebie
 * @date 2019-09-15
 */
@RestController
@RequestMapping("/goodscategory")
public class GoodsCategoryController {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    /**
     * 获取分类列表
     * @return
     */
    @PostMapping(value = "list")
    public ResultBody list() {
        QueryWrapper<GoodsCategory> goodsCategoryQueryWrapper =  new QueryWrapper<>();
        goodsCategoryQueryWrapper.eq("parent_id",0);
        goodsCategoryQueryWrapper.eq("deleted",0);
        List<GoodsCategory> goodsCategoryList = goodsCategoryService.list(goodsCategoryQueryWrapper);
        List<GoodsCategoryVO> goodsCategoryVOList = Lists.newArrayList();
        for(GoodsCategory goodsCategory:goodsCategoryList){
            GoodsCategoryVO goodsCategoryVO = BeanConvertUtils.convertBean(goodsCategory,GoodsCategoryVO.class);
            QueryWrapper<GoodsCategory> goodsCategoryQueryWrapper1 =  new QueryWrapper<>();
            goodsCategoryQueryWrapper1.eq("parent_id",goodsCategory.getCategoryId());
            goodsCategoryQueryWrapper1.eq("deleted",0);
            List<GoodsCategory> goodsCategoryList1 = goodsCategoryService.list(goodsCategoryQueryWrapper1);
            goodsCategoryVO.setGoodsCategoryList(goodsCategoryList1);
            goodsCategoryVOList.add(goodsCategoryVO);
        }
        return ResultBodyUtils.ok(goodsCategoryVOList);
    }

//    /**
//     * 根据ID查找数据
//     * @param id
//     * @return
//     */
//    @GetMapping("get")
//    public ResultBody<GoodsCategory> get(@RequestParam("id") Long id) {
//        GoodsCategory entity = goodsCategoryService.getById(id);
//        return ResultBodyUtils.ok(entity);
//    }
//
//    /**
//     * 添加数据
//     * @param parentId 父类id：0表示顶级,
//     * @param name 分类名称,
//     * @param deleted 删除状态：0没有删除1已被删除
//     * @return
//     */
//    @PostMapping("add")
//    public ResultBody add(
//        @RequestParam(value = "parentId", required = false) Long parentId,
//        @RequestParam(value = "name", required = false) String name,
//        @RequestParam(value = "deleted", required = false) Integer deleted
//    ) {
//        GoodsCategory entity = new GoodsCategory();
//        entity.setParentId(parentId);
//        entity.setName(name);
//        entity.setDeleted(deleted);
//        goodsCategoryService.save(entity);
//        return ResultBodyUtils.ok();
//    }
//
//    /**
//     * 更新数据
//     * @param parentId 父类id：0表示顶级
//     * @param name 分类名称
//     * @param deleted 删除状态：0没有删除1已被删除
//     * @return
//     */
//    @PostMapping("update")
//    public ResultBody update(
//            @RequestParam(value = "categoryId", required = false) Long categoryId,
//            @RequestParam(value = "parentId", required = false) Long parentId,
//            @RequestParam(value = "name", required = false) String name,
//            @RequestParam(value = "deleted", required = false) Integer deleted
//    ) {
//        GoodsCategory entity = new GoodsCategory();
//        entity.setCategoryId(categoryId);
//        entity.setParentId(parentId);
//        entity.setName(name);
//        entity.setDeleted(deleted);
//        goodsCategoryService.updateById(entity);
//        return ResultBodyUtils.ok();
//    }
//
//    /**
//     * 删除数据
//     * @param id 单个id
//     * @param ids 多个id用,号隔开
//     * @return
//     */
//    @PostMapping("remove")
//    public ResultBody remove(
//            @RequestParam(value = "id", required = false) Long id,
//            @RequestParam(value = "ids", required = false) String ids
//    ) {
//        if(id != null && id > 0) {
//            goodsCategoryService.removeById(id);
//        }
//        if(ids != null && ids.length() > 0) {
//            goodsCategoryService.removeByIds(Arrays.asList(ids.split(",")));
//        }
//        return ResultBodyUtils.ok();
//    }

}
